package com.example.startproject203jr;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.startproject203jr.Listener.WeatherListener;
import com.example.startproject203jr.bean.WeatherInfo;
import com.example.startproject203jr.modol.WeatherModol;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,WeatherListener{
    private EditText cityNOInput;
    private TextView city;
    private TextView cityNO;
    private TextView temp;
    private TextView wd;
    private TextView ws;
    private TextView sd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cityNOInput = (EditText) findViewById(R.id.et_city_no);
        city = (TextView) findViewById(R.id.tv_city);
        cityNO = (TextView) findViewById(R.id.tv_city_no);
        temp = (TextView) findViewById(R.id.tv_temp);
        wd = (TextView) findViewById(R.id.tv_WD);
        ws = (TextView) findViewById(R.id.tv_WS);
        sd = (TextView) findViewById(R.id.tv_SD);

        findViewById(R.id.button1).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                WeatherModol weatherModel = new WeatherModol();
                weatherModel.getWeather(cityNOInput.getText().toString(), this);
                break;
        }
    }

    @Override
    public void onResponse(WeatherInfo weather) {
        if (weather != null) {
            city.setText(weather.getCity());
            cityNO.setText(weather.getCityid());
            temp.setText(weather.getTemp());
            wd.setText(weather.getWD());
            ws.setText(weather.getWS());
            sd.setText(weather.getSD());
        } else {
            city.setText("未知");
        }
    }

    @Override
    public void onFail(String msg) {
        city.setText(msg);
    }
}
